/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */


package ru.teleology.cubefs.api.reflection;

import java.util.AbstractList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public abstract class TypedListBase<E> extends Typed implements List<E> {
	
	protected AbstractList<E> delegate = new AbstractList<E>() {
		@Override
		public E get(int index) {
			return TypedListBase.this.get0(index);
		}

		@Override
		public int size() {
			return TypedListBase.this.size();
		}
		
		@Override
		public E remove(int index) {
			return TypedListBase.this.remove(index);
		}
		
		public void add(int index, E element) {
			TypedListBase.this.add(index, element);
		}
	};
	
	public TypedListBase(long handle) {
		super(handle, true);
	}
	
	public TypedListBase(long handle, boolean weak) {
		super(handle, weak);
	}
	
	abstract protected E get0(int index);
	abstract public void add(int index, E element);
	abstract public E set(int index, E element);

	@Override
	public int size() {
		return size(getHandle());
	}
	
	
	public boolean add(E e) {
		return delegate.add(e);
	}

	@Override
	public boolean addAll(Collection<? extends E> c) {
		return delegate.addAll(c);
	}

	@Override
	public boolean addAll(int index, Collection<? extends E> c) {
		return delegate.addAll(index, c);
	}

	@Override
	public void clear() {
		Clear(getHandle());
	}

	@Override
	public boolean contains(Object o) {
		return delegate.contains(o);
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		return delegate.containsAll(c);
	}

	@Override
	public int indexOf(Object o) {
		return delegate.indexOf(o);
	}

	@Override
	public boolean isEmpty() {
		return delegate.isEmpty();
	}

	@Override
	public Iterator<E> iterator() {
		return delegate.iterator();
	}

	@Override
	public int lastIndexOf(Object o) {
		return delegate.lastIndexOf(o);
	}

	@Override
	public ListIterator<E> listIterator() {
		return delegate.listIterator();
	}

	@Override
	public ListIterator<E> listIterator(int index) {
		return delegate.listIterator(index);
	}

	@Override
	public boolean remove(Object o) {
		return delegate.remove(o);
	}

	@Override
	public E remove(int index) {
		E element = get(index);
		Remove(getHandle(), index);
		return element;
	}
	
	public void removeAll(int from , int to) {
		RemoveAll(getHandle(), from, to);
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		return delegate.removeAll(c);
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		return delegate.retainAll(c);
	}

	@Override
	public List<E> subList(int fromIndex, int toIndex) {
		return delegate.subList(fromIndex, toIndex);
	}

	@Override
	public Object[] toArray() {
		return delegate.toArray();
	}

	@Override
	public <T> T[] toArray(T[] a) {
		return delegate.toArray(a);
	}
	
	@Override
	public String toString() {
		return delegate.toString();
	}
	
	protected static native long newList(int code);
	private static native int size(long me);
	private static native void Clear(long me);
	private static native void Remove(long me, int index);
	private static native void RemoveAll(long me, int from, int to);
}
